package org.infodavid.common.lang;

import java.lang.reflect.Constructor;
import java.nio.charset.Charset;

import org.infodavid.common.impl.lang.SerializerImpl;

/**
 * The Class Serializer.
 */
public abstract class Serializer {

  /** Instance of persistence provider. */
  private static Serializer instance;

  static {
    try {
      final Constructor<SerializerImpl> constructor = SerializerImpl.class.getDeclaredConstructor();

      constructor.setAccessible(true);
      instance = constructor.newInstance();
    } catch (final Exception e) {
      throw new Error("Can't instantiate a new Serializer", e);
    }
  }

  /**
   * getInstance.
   *
   * @return the serializer
   */
  public static Serializer getInstance() {
    return instance;
  }

  /**
   * The Constructor.
   *
   * @throws Exception
   *           the exception
   */
  protected Serializer() throws Exception {
  }

  /**
   * Deserialize.
   *
   * @param value
   *          the string value
   * @return the object
   */
  public abstract Object deserialize(String value);

  /**
   * Get Delegate .
   *
   * @return the delegate
   */
  public abstract Object getDelegate();

  /**
   * Serialize.
   *
   * @param data
   *          the context.
   * @param buffer
   *          the buffer.
   * @param encoding
   *          the encoding
   */
  public abstract void serialize(Object data, StringBuffer buffer, Charset encoding);

  /**
   * Serialize.
   *
   * @param data
   *          the context.
   * @param encoding
   *          the encoding
   * @return the string
   */
  public abstract String serialize(Object data, Charset encoding);
}
